package com.cn.task;

import com.alibaba.fastjson.JSON;
import com.cn.Utils.RunLogQueue;
import com.cn.entity.RunLog;
import com.cn.service.RunLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by chenyuncong on 2018/8/6.
 */
@Component
public class RunLogCountConsumer extends Consumer implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private RunLogService runLogService;

    @Autowired
    private MongoTemplate mongoTemplate;


    //初始化监控线程
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println(mongoTemplate);
        this.start();
    }

    @Override
    public void run() {
        System.out.println("启动了run 方法 1");

        while (true){
            try {
                RunLog runLog = RunLogQueue.queueCount.take();
                tasks(runLog);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public void tasks(RunLog runLog){
//        countMap.clear(); // countMap 记录结果map
        System.out.println("count "+countMap);
        countMap.put("a",runLog);
        System.out.println(JSON.toJSONString(runLog));
    }
}
